package com.yq.tanhua.ui.detail

import androidx.lifecycle.viewModelScope
import com.fmt.mvi.learn.gobal.log
import com.google.gson.Gson
import com.hyphenate.EMCallBack
import com.yq.tanhua.im.HxImFunction
import com.yq.tanhua.im.ImFunction
import com.yq.tanhua.net.BaseViewModel
import com.yq.tanhua.net.TanHuaApiService
import com.yq.tanhua.net.vo.Movment
import com.yq.tanhua.net.vo.TodayBest
import com.yq.tanhua.utils.HxUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody

class PersonalInfoViewModel : BaseViewModel() {


    private val personInfoUIState =
        MutableStateFlow(PersionInfoData(loadingState = CommonRequestState.Loading))
    val persionUIState = personInfoUIState.asStateFlow()

    private val _sendMsgUIState =
        MutableStateFlow(SendMsgDataState(loadingState = null))
    val sendMsgUIState = _sendMsgUIState.asStateFlow()

    private val _personMoventUIState = MutableStateFlow<List<Movment>?>(null)
    val personMovementUiState = _personMoventUIState.asStateFlow()
    var userId = ""
    fun loadData(userId: String) {
        this.userId = userId
        launch(block = {
            val todayBest = TanHuaApiService.getPersonInfo(userId).fullData()
            "请求成功".log()
            personInfoUIState.value = personInfoUIState.value.copy(
                data = todayBest,
                loadingState = CommonRequestState.LoadSuccess
            )

            loadMoventData()
        }, error = {
            "请求失败".log()
            personInfoUIState.value =
                personInfoUIState.value.copy(loadingState = CommonRequestState.LoadFail(it.message))
        })

        launch(block = {
            val question = TanHuaApiService.strangerQuestions(userId).fullData()
            personInfoUIState.update {
                it.copy(question = question)
            }
        })
    }


    private fun loadMoventData() {
        launch(
            block = {
                val movementsPageInfo = TanHuaApiService.getMovements(userId).fullData()
                _personMoventUIState.value = movementsPageInfo.items
            }
        )
    }


    fun sendChatMsg(reply: String) {
        _sendMsgUIState.update { it.copy(loadingState = CommonRequestState.Loading) }
        launch(block = {
            val map: HashMap<String, Any> = HashMap()
            map.put("userId", userId)
            map.put("reply", reply)
            val jsonParam = Gson().toJson(map)
            val body =
                jsonParam.toRequestBody("application/json;charset=utf-8".toMediaType())
            //回话列表只能获取本地数据，所以服务器发送的数据本地看不到，改为本地发送

            TanHuaApiService.strangerQuestions(body)
            _sendMsgUIState.update { it.copy(loadingState = CommonRequestState.LoadSuccess) }
            "发送消息成功".log()

//            val imFunction = HxImFunction()
//            imFunction.sendTextMsg(reply, HxUtils.getHuanXinId(userId), object : EMCallBack {
//                override fun onSuccess() {
//                    viewModelScope.launch {
//                        TanHuaApiService.strangerQuestions(body)
//                        _sendMsgUIState.update { it.copy(loadingState = CommonRequestState.LoadSuccess) }
//                        "发送消息成功".log()
//                    }
//                }
//
//                override fun onError(code: Int, error: String?) {
//
//                }
//
//            })

        }, error = { apiEx ->
            apiEx.printStackTrace()
            _sendMsgUIState.update { it.copy(loadingState = CommonRequestState.LoadFail(apiEx.message)) }
            "发送消息失败".log()
        })
    }
}

data class PersionInfoData(
    val data: TodayBest? = null,
    val question: String? = null,
    val loadingState: CommonRequestState
)

data class SendMsgDataState(val loadingState: CommonRequestState?)


sealed class CommonRequestState {
    object Loading : CommonRequestState()
    object LoadSuccess : CommonRequestState()
    class LoadFail(var msg: String) : CommonRequestState()
}

